﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ProviderLoader.Configuration;
using PSE.Framework.Configuration;
using PSE.Framework.ProviderLoader;
using System.Configuration;

namespace PSE.Framework.UI.Security.Authentication
{
    internal class AuthenticationProviderFactory
    {
        private const string CONFIG_SECTION_NAME = "clientProviders";
        private const string PROVIDER_NAME = "authenticationProvider";

        /// <summary>
        /// Retorna o provider de Autenticação
        /// </summary>
        /// <returns>Instância do provider de autenticação configurado.</returns>
        public static IAuthenticationProvider CreateProvider()
        {
            try
            {
                ProviderConfigurationSection configSection = ApplicationConfiguration.GetCurrent().GetSection<ProviderConfigurationSection>(CONFIG_SECTION_NAME);

                if (configSection != null)
                    return ProviderLoader<IAuthenticationProvider>.LoadProvider(configSection.Providers, PROVIDER_NAME);
                else
                    return null;
            }
            catch (Exception ex)
            {
                //System.Diagnostics.EventLog.WriteEntry("AuthenticationProviderFactory", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                throw new ConfigurationErrorsException("Could not create an instance of Authetication Provider configured.", ex);
            }
        }
    }
}
